package core.network.client2game.handler.nn;

import business.global.pk.nn.NNRoom;
import business.global.room.RoomMgr;
import business.global.room.base.AbsBaseRoom;
import business.global.room.base.AbsRoomPos;
import business.player.Player;
import com.ddm.server.websocket.def.ErrorCode;
import com.ddm.server.websocket.handler.requset.WebSocketRequest;
import com.google.gson.Gson;
import core.network.client2game.handler.room.CBaseGameStartGame;
import core.network.http.proto.SData_Result;
import jsproto.c2s.iclass.room.CBase_RoomID;

import java.io.IOException;
import java.util.Objects;


/**
 * 开始游戏
 */
public class CNNStartGame extends CBaseGameStartGame {

    @SuppressWarnings("rawtypes")
    @Override
    public void handle(Player player, WebSocketRequest request, String message) throws IOException {
        final CBase_RoomID req = new Gson().fromJson(message, CBase_RoomID.class);

        NNRoom room = (NNRoom) RoomMgr.getInstance().getRoom(req.getRoomID());
        if (Objects.isNull(room)) {
            request.error(ErrorCode.NotAllow, "CBaseGameStartGame not find room:" + req.getRoomID());
            return;
        }
        //如果房主开始，检查当前player是否是房主，如果是首家开始，检查当前人是否是首家，其他情况返回error
        if (room.roomCfg.youxikaishi == 0) {
            AbsRoomPos pos = room.getRoomPosMgr().getPosByPosID(0);
            if (pos == null || pos.getPid() != player.getPid()) {
                request.error(ErrorCode.NotAllow, "CNNStartGame shouweikaishi:" + req.getRoomID());
                return;
            }
        } else if (room.roomCfg.youxikaishi == 1 && room.getOwnerID() != player.getPid() && room.getOwnerID() > 0) {
            request.error(ErrorCode.NotAllow, "CNNStartGame fangzhukaishi:" + req.getRoomID());
            return;
        } else if (room.roomCfg.youxikaishi >= 2) {
            request.error(ErrorCode.NotAllow, "CNNStartGame manrenkaishi:" + req.getRoomID());
            return;
        }
        // 开始游戏
        super.handle(player, request, message);
    }
}			
